home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk14 / epsstp / stp.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  5KB  |  183 lines

  1. #include <stdio.h>
  2. #include <exec/types.h>
  3. #include <exec/exec.h>
  4. #include <libraries/dos.h>
  5. #include <libraries/dosextens.h>
  6. #include <exec/memory.h>
  7. #include <exec/execbase.h>
  8. #include <exec/tasks.h>
  9. #include <exec/ports.h>
  10. #include <exec/io.h>
  11.  
  12. /*
  13.     Program:  stp.c  (Set Task Priority)
  14.     Purpose:  Change a currently existing task's priority.
  15.     Author:   Jeffrey Bailey
  16.     Date:     3/15/88
  17.     Usage:    stp <hex TCB address> <dec. priority>
  18.     Compiler: Lattice C 4.0
  19.     Porting:  It should be relatively easy to modify this to work
  20.               with Aztec C.  The only special routine I used was
  21.               strtol(), which may or may not exist in Aztec.
  22.  
  23.     I release this program into the public domain.  You may use all or 
  24.     part of the source code for any purpose whatsoever.  All I ask is 
  25.     that you give credit where credit is due.  
  26.  
  27.     I do not guarantee this program in any way, shape, or form.  Note
  28.     that it does work for me, and I have attempted to test it
  29.     thoroughly.  Use it at your own risk.  You can report
  30.     any bugs, problems, features, suggestions, etc. to the address below.
  31.     I do not promise to change or fix anything, but I will try to 
  32.     respond to any mail messages I receive - please include a good 
  33.     return path in your message!
  34.  
  35.     UUCP:...{ihnp4!codas, ucf-cs, allegra}!novavax!jeff
  36.     UUCP:...{ihnp4!codas, ucf-cs, allegra}!novavax!regulus!jeff  (Private)
  37.         (routing through codas preferred)
  38. */
  39.  
  40. struct ExecBase *ExecBase;
  41.  
  42. void main(argc, argv)
  43.     int  argc;
  44.     char *argv [];
  45.     {
  46.     char *badchar;
  47.     struct Task *tad;
  48.     long tpri;
  49.     int  saneaddr;
  50.  
  51. /*
  52.     If started from WorkBench, exit.  Don't bother printing an error msg.
  53. */
  54.     if(argc == 0) exit(20);
  55.  
  56.     if(argc < 3)
  57.         {
  58.         puts("STP:  Missing args.  Supply a task address and a priority.");
  59.         exit(20);
  60.         }
  61.  
  62.     if(argc > 3)
  63.         {
  64.         puts("STP:  Too many args.  Supply a task address and a priority.");
  65.         }
  66.  
  67. /*
  68.     Convert hex string to a pointer value.  If strtol() sets badchar to point
  69.     to a NULL, then conversion went ok.  Otherwise, badchar points at char in
  70.     error.
  71. */
  72.     tad = (struct Task *) strtol(argv [1], &badchar, 16);
  73.  
  74.     if(*badchar != NULL)
  75.         {
  76.         puts("STP:  Bad hex string conversion on task address.");
  77.         exit(20);
  78.         }
  79.  
  80. /*
  81.     Convert decimal string to an integer value.  If strtol() sets badchar to 
  82.     point to a NULL, then conversion went ok.  Otherwise, badchar points at 
  83.     char in error.
  84. */
  85.     tpri = strtol(argv [2], &badchar, 10);
  86.  
  87.     if(*badchar != NULL)
  88.         {
  89.         puts("STP:  Bad decimal string conversion on task priority.");
  90.         exit(20);
  91.         }
  92.  
  93. /*
  94.     Check priority to make sure it's within proper bounds.
  95. */
  96.     if(tpri < -128)
  97.         {
  98.         puts("STP:  Priority specified is too low.  Must be no less than -128.");
  99.         exit(20);
  100.         }
  101.     if(tpri > 127)
  102.         {
  103.         puts("STP:  Priority specified is too high.  Must be no greater than 127.");
  104.         exit(20);
  105.         }
  106.  
  107. /*
  108.     Get ExecBase pointer.  We need this to do sanity checking on the TCB
  109.     address supplied.
  110. */
  111.     if( (ExecBase = (struct ExecBase *) OpenLibrary("exec.library",0)) == 0 )
  112.         {
  113.         puts("STP:  Couldn't open Exec Library.");
  114.         exit(20);
  115.         }
  116.  
  117. /*
  118.     Disable interrupts.  The RKM says this is necessary when scanning exec
  119.     lists.  Apparently Forbid() isn't good enough in this instance.
  120. */
  121.     Disable();
  122.  
  123. /*
  124.     Do our TCB address sanity checking.  Set the new task priority if the
  125.     address is valid.
  126. */
  127.     if( (saneaddr = sane_address(tad)) )
  128.         {
  129.         SetTaskPri(tad, tpri);
  130.         }
  131.  
  132. /*
  133.     Enable interrupts again.
  134. */
  135.     Enable();
  136.  
  137. /*
  138.     If we couldn't find the TCB, complain.  Can't do this inside the Disable()
  139.     Enable() pair, 'cause we can't keep interrupts disabled for very long, plus
  140.     the RKM warns against printing when you disable interrupts.
  141. */
  142.     if(!saneaddr)
  143.         {
  144.         puts("STP:  Can't find TCB at specified address.");
  145.         }
  146.  
  147.     exit(0);
  148.     }
  149.  
  150. sane_address(taskaddr)
  151.     register struct Task *taskaddr;
  152.     {
  153.     register struct Task *task;
  154.  
  155. /*
  156.     Check to see if we're trying to modify the current task's priority.
  157. */
  158.     if(taskaddr == ExecBase->ThisTask) return(1);
  159.  
  160. /*
  161.     Check the ready queue for our TCB.
  162. */
  163.     for(task = (struct Task *)ExecBase->TaskReady.lh_Head; task->tc_Node.ln_Succ;
  164.         task = (struct Task *)task->tc_Node.ln_Succ)
  165.         {
  166.         if(task == taskaddr) return(1);
  167.         }
  168.  
  169. /*
  170.     Check the wait queue for our TCB.
  171. */
  172.     for(task = (struct Task *)ExecBase->TaskWait.lh_Head; task->tc_Node.ln_Succ;
  173.         task = (struct Task *)task->tc_Node.ln_Succ)
  174.         {
  175.         if(task == taskaddr) return(1);
  176.         }
  177.  
  178. /*
  179.     If we got to this point, we haven't found our TCB.  Return failure.
  180. */
  181.     return(0);
  182.     }
  183.